package com.seanchenxi.gwt.xmlrpc.client;

import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * The Class AbstractXmlRpcClient.
 */
public abstract class AbstractXmlRpcClient implements Cloneable {

	/** The config. */
	XmlRpcClientConfig config;

	/**
	 * Instantiates a new abstract xml rpc client.
	 * 
	 * @param url
	 *            the xml rpc url
	 */
	AbstractXmlRpcClient(String url) {
		setConfig(XmlRpcObjectFactory.createXmlRpcClientConfig(url));
		assert (getConfig() != null);
	}

	/**
	 * Send.
	 * 
	 * @param <T>
	 *            the generic type
	 * @param request
	 *            the request
	 * @param callback
	 *            the callback
	 * @throws XmlRpcException
	 *             the xml rpc exception
	 */
	abstract <T> void send(XmlRpcRequest<T> request, AsyncCallback<T> callback)
			throws XmlRpcException;

	/**
	 * Send.
	 * 
	 * @param <T>
	 *            the generic type
	 * @param request
	 *            the request
	 * @param callback
	 *            the callback
	 * @param responseHandler
	 *            the response handler
	 * @throws XmlRpcException
	 *             the xml rpc exception
	 */
	abstract <T> void send(XmlRpcRequest<T> request, AsyncCallback<T> callback,
			ResponseHandler<? extends T> responseHandler)
			throws XmlRpcException;

	/**
	 * Send.
	 * 
	 * @param <T>
	 *            the generic type
	 * @param xmlRpcRequest
	 *            the xml rpc request
	 * @param requestCallBack
	 *            the request call back
	 * @throws RequestException
	 *             the request exception
	 */
	abstract <T> void send(XmlRpcRequest<T> xmlRpcRequest,
			RequestCallback requestCallBack) throws RequestException;

	/**
	 * Gets the config.
	 * 
	 * @return the config
	 */
	XmlRpcClientConfig getConfig() {
		return config;
	}

	/**
	 * Sets the config.
	 * 
	 * @param config
	 *            the new config
	 */
	void setConfig(XmlRpcClientConfig config) {
		this.config = config;
	}

	// /**
	// * Gets the xml rpc path.
	// *
	// * @return the xml rpc path
	// */
	// String getXmlRpcPath(){
	// if(getClass().getAnnotation(XmlRpcRelativePath.class) == null) return
	// null;
	// String path = getClass().getAnnotation(XmlRpcRelativePath.class).value();
	// if(path == null || path.length() < 1) return null;
	// return GWT.getModuleBaseURL().concat(path);
	// }
}
